package com.hand.wfive.view;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PointF;
import android.text.TextUtils;
import android.util.Log;

import com.hand.wfive.App;

import cn.hzw.doodle.DoodleBitmap;
import cn.hzw.doodle.DoodleColor;
import cn.hzw.doodle.DoodleOnTouchGestureListener;
import cn.hzw.doodle.DoodleText;
import cn.hzw.doodle.DoodleTouchDetector;
import cn.hzw.doodle.DoodleView;
import cn.hzw.doodle.IDoodleListener;
import cn.hzw.doodle.core.IDoodle;
import cn.hzw.doodle.core.IDoodleItem;
import cn.hzw.doodle.core.IDoodlePen;
import cn.hzw.doodle.core.IDoodleSelectableItem;


@SuppressLint("ViewConstructor")
public class CustomDoodleView extends DoodleView {

    private static final String TAG = CustomDoodleView.class.getSimpleName();

    private final DoodleOnTouchGestureListener mTouchGestureListener;
    private IDoodleSelectableItem mSelectableItem = null;

    public CustomDoodleView(Context context, Bitmap bitmap, IDoodleListener listener) {
        super(context, bitmap, listener);
        mTouchGestureListener = new DoodleOnTouchGestureListener(this, new DoodleOnTouchGestureListener.ISelectionListener() {
            @Override
            public void onSelectedItem(IDoodle doodle, IDoodleSelectableItem selectableItem, boolean selected) {
                Log.d(TAG, "onSelectedItem: ");
                mSelectableItem = selectableItem;
                if (mListener != null) {
                    mListener.selectItem(selectableItem, selected);
                }
            }

            @Override
            public void onCreateSelectableItem(IDoodle doodle, float x, float y) {
                Log.d(TAG, "onCreateSelectableItem: ");
            }

           /* @Override
            public void onDelSelectableItem() {
                if (mListener != null) {
                    mListener.delItem();
                }
            }*/
        });
        setDefaultTouchDetector(new DoodleTouchDetector(context, mTouchGestureListener));
    }

    @Override
    public void setPen(IDoodlePen pen) {
        super.setPen(pen);
        mTouchGestureListener.setSelectedItem(null);
    }

    @Override
    public void setSize(float paintSize) {
        super.setSize(paintSize);
        if (mTouchGestureListener.getSelectedItem() != null) {
            mTouchGestureListener.getSelectedItem().setSize(getSize());
        }
    }

    public IDoodleSelectableItem getSelectItem() {
        if (null != mTouchGestureListener) {
            return mTouchGestureListener.getSelectedItem();
        }
        return null;
    }

    @Override
    public boolean undo() {
        Log.d(TAG, "undo: ");
        boolean result = super.undo();
        mTouchGestureListener.setSelectedItem(null);
        refreshUndoAnRedo();
        return result;
    }

    @Override
    public boolean redo(int step) {
        Log.d(TAG, "redo: ");
        boolean result = super.redo(step);
        mTouchGestureListener.setSelectedItem(null);
        refreshUndoAnRedo();
        return result;
    }

    @Override
    public void clear() {
        Log.d(TAG, "clear: ");
        super.clear();
        mTouchGestureListener.setSelectedItem(null);
        refreshUndoAnRedo();
    }

    @Override
    public void addItem(IDoodleItem item) {
        Log.d(TAG, "addItem: ");
        super.addItem(item);
        refreshUndoAnRedo();
    }

    @Override
    public void removeItem(IDoodleItem doodleItem) {
        Log.d(TAG, "removeItem: ");
        super.removeItem(doodleItem);
        mTouchGestureListener.setSelectedItem(null);
        refreshUndoAnRedo();
    }

    @Override
    public void topItem(IDoodleItem item) {
        Log.d(TAG, "topItem: ");
        super.topItem(item);
        refreshUndoAnRedo();
    }

    @Override
    public void bottomItem(IDoodleItem item) {
        Log.d(TAG, "bottomItem: ");
        super.bottomItem(item);
        refreshUndoAnRedo();
    }

    private void refreshUndoAnRedo() {
        if (mListener != null) {
            mListener.redo(getRedoItemCount() > 0);
            mListener.undo(getItemCount() > 0);
        }
    }

    public void removeSelectableItem() {
        if (mSelectableItem != null) {
            removeItem(mSelectableItem);
        }
    }

    /**
     * 图片贴纸
     */
    public IDoodleSelectableItem createDoodleBitmap(Bitmap bitmap) {
        if (bitmap == null || bitmap.isRecycled()) return null;
        PointF center = getCenterPoint();
        return createDoodleBitmap(bitmap, center.x, center.y);
    }

    public IDoodleSelectableItem createDoodleBitmap(Bitmap bitmap, float x, float y) {
        if (bitmap == null || bitmap.isRecycled()) return null;
        try {
            float bitmapMinSize = Math.min(bitmap.getWidth(), bitmap.getHeight());
            float viewMinSize = Math.min(getCenterWidth(), getCenterHeight());
            float size = viewMinSize > bitmapMinSize ? bitmapMinSize : viewMinSize / 2f;
            IDoodleSelectableItem item = new DoodleBitmap(
                    this, bitmap, size, x - size / 2f,
                    y - (bitmap.getHeight() * size / bitmap.getWidth()) / 2f);
            addItem(item);
            mTouchGestureListener.setSelectedItem(item);
            refresh();
            return item;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 文字贴纸
     */
    public void createDoodleText(String content) {
        if (TextUtils.isEmpty(content)) return;
        PointF center = getCenterPoint();
        try {
            Paint paint = new Paint();
            paint.setTextSize(100f);
            paint.setTypeface(App.getContext().getSongTypeface());
            IDoodleSelectableItem item = new DoodleText(
                    this, content, 100f, new DoodleColor(Color.BLACK),
                    App.getContext().getSongTypeface(),
                    center.x - (paint.measureText(content) / 2), center.y - 50f);
            addItem(item);
            mTouchGestureListener.setSelectedItem(item);
            refresh();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public PointF getCenterPoint() {
        float centerX = (getCenterWidth() / 2f - getDoodleTranslationX()) / getAllScale();
        float centerY = (getCenterHeight() / 2f - getDoodleTranslationY()) / getAllScale();
        Log.d(TAG, "getCenterPoint: " + centerX + ", " + centerY);
        return new PointF(centerX, centerY);
    }

    private Listener mListener;

    public void setListener(Listener listener) {
        mListener = listener;
    }

    public interface Listener {

        void undo(boolean canUndo);

        void redo(boolean canRedo);

        void delItem();

        void selectItem(IDoodleSelectableItem selectableItem, boolean isSelect);
    }
}
